package com.fone.player.activity.local.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.activity.local.LocalActivity;
import com.fone.player.cache.ImageDownloadModule;
import com.fone.player.entity.MediaFile;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.util.ScreenUtil;
import com.fone.player.util.SystemUtil;
import com.fone.player.view.LocalDeleteMediaDialog;
import com.fone.player.view.LocalDeleteMediaDialog.IDeleteListener;
import com.fone.player.view.LocalVedioOperationPopWindow;
import com.fone.player.view.LocalVedioOperationPopWindow.IOperationListener;

public class LocalMediaAdatper extends BaseAdapter {

	private List<com.fone.player.entity.MediaFile> mVideos;
	private Activity mActivity;
	private List<Boolean> mCheckBoxs;
	private String TAG = "LocalMediaAdatper";
	private ListView mListView;
	private Handler mHandler;
	private int width;
	private int height;

	@SuppressLint("HandlerLeak")
	public LocalMediaAdatper(Activity mActivity, List<MediaFile> Videos,
			List<Boolean> checkBoxs, ListView listview, Handler mHandler) {
		this.mActivity = mActivity;
		this.mVideos = Videos;
		this.mCheckBoxs = checkBoxs;
		this.mListView = listview;
		this.mHandler = mHandler;
		width = ScreenUtil.dp2px(140.0f);
		height = ScreenUtil.dp2px(80.0f);

		mListView.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {

			}
		});
	}

	@Override
	public int getCount() {
		int count = 0;
		if (mVideos != null) {
			count = mVideos.size();
		}
		return count;
	}

	@Override
	public Object getItem(int position) {
		return mVideos.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mActivity,
					R.layout.local_list_video_item, null);

			holder.selectedCb = (CheckBox) convertView
					.findViewById(R.id.cb_local_selected);
			holder.itemName = (TextView) convertView
					.findViewById(R.id.tv_local_item_name);
			holder.totalSize = (TextView) convertView
					.findViewById(R.id.tv_local_item_size);
			holder.videoImage = (ImageView) convertView
					.findViewById(R.id.iv_local_vedio);
			holder.moreImage = (ImageView) convertView
					.findViewById(R.id.iv_local_option);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (mCheckBoxs.get(0)) {
			holder.selectedCb.setVisibility(View.VISIBLE);
			holder.moreImage.setVisibility(View.GONE);

		} else {
			holder.selectedCb.setVisibility(View.GONE);
			holder.moreImage.setVisibility(View.VISIBLE);
		}
		holder.selectedCb.setClickable(false);
		holder.selectedCb
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						mVideos.get(position).setDeleteSelected(isChecked);
					}
				});
		holder.moreImage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				final LocalVedioOperationPopWindow popWindow = new LocalVedioOperationPopWindow(
						mActivity, true);
				((ImageView) v)
						.setImageResource(R.drawable.quick_more_small_click);
				popWindow.setOperationListener(new IOperationListener() {
					@Override
					public void delete() {
						// 判断存储设备是否挂载
						if (!SystemUtil.getInstance().isStorageMounted()) {
							FoneUtil.showToast(mActivity,
									R.string.no_sdcard_mounted_text);
							return;
						}
						deleteFile(mVideos, position);
					}

					@Override
					public void encryOrdecode() {
						if (hasPassWord()) {

							Message msg = mHandler.obtainMessage();
							msg.what = LocalActivity.MSG_ENCRYPT_MEDIA;
							msg.arg1 = LocalActivity.mCurrentPageID;// 当前页面
							msg.arg2 = position;
							mHandler.sendMessage(msg);
						} else {
							// 当没有创建私人库 需要进入到私人库页面设置密码时
							Message message = mHandler.obtainMessage();
							message.what = LocalActivity.MSG_SCROLL_VIEW_PAGER;
							message.arg1 = LocalActivity.PAGE_PRIVATE_MEDIAS;
							message.arg2 = position;
							message.obj = LocalActivity.mCurrentPageID;// 当前页面
							mHandler.sendMessage(message);

						}

					}

					@Override
					public void dismiss() {
						((ImageView) v)
								.setImageResource(R.drawable.quick_more_small);
					}
				});
				popWindow.showPopupWindow(v);
			}
		});

		MediaFile itemBean = (MediaFile) mVideos.get(position);

		holder.itemName.setText(itemBean.getMediaFileNameSuffix());
		holder.totalSize.setText((Formatter.formatFileSize(mActivity,
				itemBean.getMediaFileSize())));
		holder.videoImage.setImageResource(R.drawable.default_340_192);
		ImageDownloadModule.getInstance().display(itemBean.getMediaFilePath(),
				width, height, holder.videoImage);
		// holder.videoImage.setTag(R.drawable.default_160);
		L.i(TAG, "itemBean.path:" + itemBean.getMediaFilePath());
		holder.selectedCb.setChecked(itemBean.isDeleteSelected());

		return convertView;
	}

	class ViewHolder {
		public TextView itemName, totalSize;
		public ImageView videoImage;
		public ImageView moreImage;
		public CheckBox selectedCb;
	}

	public void refreshMediaView(String path, Bitmap bitmap) {
		ImageView imageViewByTag = (ImageView) mListView.findViewWithTag(path);
		L.v(TAG, "refreshMediaView postion", "path:" + path);
		if (imageViewByTag != null) {

			if (bitmap != null) {
				imageViewByTag.setImageBitmap(bitmap);
			} else {
				imageViewByTag.setImageDrawable(mActivity.getResources()
						.getDrawable(R.drawable.default_220_292));
			}

		}
	}

	private boolean hasPassWord() {
		String FILE_NAME = "FILE_PASSWORSD";
		String LOCAL_PASSWORD = "local_password";
		SharedPreferences sp = mActivity.getSharedPreferences(FILE_NAME,
				Context.MODE_PRIVATE);
		String localPassword = sp.getString(LOCAL_PASSWORD, null);
		L.i(TAG, "localPassword:" + localPassword);
		return !TextUtils.isEmpty(localPassword);
	}

	// 删除文件
	private void deleteFile(List<MediaFile> list, final int position) {
		LocalDeleteMediaDialog deleteDialog = new LocalDeleteMediaDialog(
				mActivity);
		deleteDialog.setListener(new IDeleteListener() {

			@Override
			public void delete() {
				Message message = mHandler.obtainMessage();
				message.what = LocalActivity.MSG_DELETE_MEDIA;
				message.arg1 = position;
				message.arg2 = LocalActivity.mCurrentPageID;
				mHandler.sendMessage(message);
			}
		});
	}

}
